﻿//根据文件夹名称获取文件列表
function GetImgs(_FID) {
    var obj = $("#tabDir a[FID='" + _FID + "']");
    $(".selected").removeClass("selected");
    obj.parent().parent().addClass("selected");
    $("#hfFID").val(_FID);
    GetPages(_FID);
    GetData();
}

//后台获取文件列表并输出
function GetPages(_FID) {
    $.ajax({
        type: "post",
        url: "ashx/VolumeDir.ashx",
        dataType: "json",
        async: false,
        data: { type: "GetPages", FID: _FID },
        success: function (msg) {
            if (msg.result == 1) {
                $("#tabImg tbody").html(msg.strHtml);
                var len = $("#tabImg tbody tr").length;
                if (len > 0) {
                    $("#tabImg tbody tr:eq(0)").click();
                }
                else {
                    jQuery("#viewer").iviewer('loadImage', "");//更新
                }
            }
        }
    });
}

//增加一行
function AddTr() {
    //AddData1();
    //var count = $(".selected").length;
    //if (count == 0) { showTip("请先选择一份档案!", 2, 0); return false; }

    var len = $("#tab tbody tr").length + 1;
    //var trHtml = "<tr>";
    //trHtml += "<td class=\"text-center\">" + len + "</td>";
    //// trHtml += "<td><input id=\"Text1\" type=\"text\" class=\"form-control\"  autocomplete=\"off\" /></td>";
    //trHtml += "<td><textarea id=\"Text1\" type=\"text\"  class=\"form-control\"></textarea></td>";
    //trHtml += "<td><input id=\"Text2\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
    //trHtml += "<td><textarea  id=\"Text3\" type=\"text\" row=\"3\" class=\"form-control\"onkeyup=\"anxiafangfa(this)\"></textarea></td>";
    //trHtml += "<td><input id=\"Text4\" type=\"text\" class=\"form-control text-center\" autocomplete=\"off\" /></td>";
    //trHtml += "<td><input id=\"Text5\" type=\"text\" class=\"form-control text-center\" dataType=\"null,zzs\" Msg=\"页号格式不正确!\" autocomplete=\"off\" /></td>";
    //trHtml += "<td><input id=\"Text6\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
    //trHtml += "</tr>";
    var tpl = document.getElementById('tpl').innerHTML;
    var trHtml = juicer(tpl, { 'len': len});
    $("#tab tbody").append(trHtml);

    SetMenu2();
}
//诉讼卷内目录字数限制方法
function anxiafangfa(zzz) {
    $("#Text3").removeAttr('maxlength')
    //if (zzz != null) {
    //    $(zzz).attr("maxlength", "80")
    //    if ($(zzz).val().length >= 80) {
    //        showTip("字数超过限制!", 2, 0);
    //    }
    //}
}
//减少一行
function DelTr() {
    //var count = $(".selected").length;
    //if (count == 0) { showTip("请先选择一份档案!", 2, 0); return false; }

    $("#tab tbody tr:last").remove();

    SetMenu2();
}

//添加目录数据
function AddData() {
    if (verfic('tip', '#tab') == false) { return false; }

    var bool = true;

    _Confirm("确定要提交编目好的信息？", function () {
        //确定
        var _FID = $(".selected a").attr("FID");
        var _trData = "";
        $("#tab tbody tr").each(function (i) {
            var obj = $(this).find(".form-control");
            var str1 = obj.eq(0).val();//文号
            var str2 = obj.eq(1).val();//责任者
            var str3 = obj.eq(2).val();//文件名称
            var str4 = obj.eq(3).val();//日期
            var str5 = obj.eq(4).val();//页号
            if (str5 < 1) {
                bool = false;
            }
            var str6 = obj.eq(5).val();//备注
            var strData = str1 + "▲" + str2 + "▲" + str3 + "▲" + str4 + "▲" + str5 + "▲" + str6;
            _trData = _trData + "❤" + strData;
        })
        _trData = _trData.replace("❤", "");
        if (bool == true) {
            $.ajax({
                type: "post",
                url: "ashx/VolumeDir.ashx",
                dataType: "json",
                async: false,
                data: { type: "AddData", FID: _FID, trData: _trData },
                success: function (msg) {
                    if (msg.result == 1) {
                        showTip("目录提交成功", 1, 2);
                       //GetPages(_FID);
                    }
                }
            });
        }
        else {
            alert("输入页码不能为0");
        }
    })
}


function AddData1() {
   // if (verfic('tip', '#tab') == false) { return false; }

  //  _Confirm("确定要提交编目好的信息？", function () {
        //确定
        var _FID = $(".selected a").attr("FID");
        var _trData = "";
        $("#tab tbody tr").each(function (i) {
            var obj = $(this).find(".form-control");
            var str1 = obj.eq(0).val();//文号
            var str2 = obj.eq(1).val();//责任者
            var str3 = obj.eq(2).val();//文件名称
            var str4 = obj.eq(3).val();//日期
            var str5 = obj.eq(4).val();//页号
            if (str5 == null || str5=='') {
                str5 = 0;
            }
            var str6 = obj.eq(5).val();//备注
            var strData = str1 + "▲" + str2 + "▲" + str3 + "▲" + str4 + "▲" + str5 + "▲" + str6;
            _trData = _trData + "❤" + strData;
        })
        _trData = _trData.replace("❤", "");
        $.ajax({
            type: "post",
            url: "ashx/VolumeDir.ashx",
            dataType: "json",
            async: false,
            data: { type: "AddData", FID: _FID, trData: _trData },
            success: function (msg) {
                //if (msg.result == 1) {
                //    //GetPages(_FID);
                //    showTip("目录提交成功", 1, 2);
                //}
            }
        });
  //  })
}

function GetData() {
   
    var _FID = $(".selected a").attr("FID");

    $.ajax({
        type: "post",
        url: "ashx/VolumeDir.ashx",
        dataType: "json",
        async: false,
        data: { type: "GetData", FID: _FID },
        success: function (msg) {
            if (msg.result == 1) {
                $("#tab tbody").html(msg.htmlStr);

                SetMenu2();
            }
        }
    });
    
}



function Search() {
    DataLoading();
    var _FType = $("#ddlFType").val();//类型
    var _FYear = $("#txtFYear").val();//年度
    var _FTerm = $("#ddlFTerm").val();//期限
    var _FRef = $("#txtFRef").val();//档号
    var _Val = $("#ddlType").val();//1未处理 2全部
    //var _Step = ($("#ddlType").val() == "1") ? _ParVal : "";//是否处理

    $.ajax({
        type: "post",
        url: "ashx/VolumeDir.ashx",
        dataType: "json",
        async: true,
        data: { type: "Search", FType: _FType, FYear: _FYear, FTerm: _FTerm, FRef: _FRef, Val: _Val },
        success: function (msg) {
            if (msg.result == 1) {
                $("#tabDir tbody").html(msg.htmlStr);
                $("#tabDir tbody tr:eq(0) a").click();
                $("#htmlTip").remove();
                setMenu();
                $(".i-checks").iCheck({
                    checkboxClass: "icheckbox_square-green",
                    radioClass: "iradio_square-green"
                })
            }
        }
    })

}

//显示图片
function ShowImg(obj, ImgPath) {
    $("#tabImg tr").removeClass("selected");
    $(obj).addClass("selected");
    jQuery("#viewer").iviewer('loadImage', ImgPath);//更新
}

$(function () {
    jQuery("#viewer").iviewer();//初始化
})

//打印目录 批量
function VolumeDirPrint(_type) {
    var FIDS = "";
    $("input[type='checkbox'].cboxSel:checked").each(function (i) {
        FIDS += "❤" + $(this).val();
    })
    FIDS = FIDS.replace("❤", "");
    store.set("VolumeDirPrintFIDS1", FIDS);
    store.set("PrintType", _type);
    if (FIDS != "") {
        showPage("批量打印目录", "VolumeDirPrint.aspx", 1000, 600, false);
    }
    else {
        showTip("请选择要打印的档案！",2,0);
    }
}

//自定义右键上下文
function setMenu() {
    var objSellAll = {
        text: "全选",
        func: function () {
            $("#tabDir tbody tr td .cboxSel").not("input:checked").parents("label").click();
        }
    },
    objSellAllNo = {
        text: "反选",
        func: function () {
            $("#tabDir tbody tr td .cboxSel").parents("label").click();
        }
    },
    objClear = {
        text: "清除",
        func: function () {
            $("#tabDir tbody tr").remove();
        }
    },
    objFeiQi = {
        text: "废件箱",
        func: function () {
            if ($("#tabDir tbody tr.selected").length > 0) {
                var fileid = $("#tabDir tbody tr.selected").attr("fid");
                var FileNo = $("#tabDir tbody tr.selected td a").text();
                window.top.showFullPage('废件箱（' + FileNo + '）', 'SeeImage.aspx?FID=' + fileid);
            }
        }
    };

    var mailMenuData = [[objSellAll, objSellAllNo, objClear, objFeiQi]];
    $("#tabDir tbody tr").smartMenu(mailMenuData, {
        name: "menu",
        beforeShow: function () {
        }
    });
}

function SetMenu2() {
    var objDelTr = {
        text: "删除当前行",
        func: function () {
            if ($("#tab tbody tr").length > 0) {
                var obj = $(this);
                _Confirm("确定要删除这一行？", function () {
                    obj.remove();
                    SetIndex();//顺号
                    SetMenu2();//绑定右键事件
                })
            }
        }
    };
    var objAddTrTop = {
        text: "向上插入行",
        func: function () {
            if ($("#tab tbody tr").length > 0) {
                //var trHtml = "<tr>";
                //trHtml += "<td class=\"text-center\">" + 0 + "</td>";
                ////  trHtml += "<td><input id=\"Text1\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
                //trHtml += "<td><textarea id=\"Text1\" type=\"text\"  class=\"form-control\"></textarea></td>";
                // trHtml += "<td><input id=\"Text2\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
                
                //trHtml += "<td><textarea id=\"Text3\" type=\"text\" row=\"3\" class=\"form-control\ onkeyup=\"anxiafangfa(this)\"   ></textarea></td>";
                //trHtml += "<td><input id=\"Text4\" type=\"text\" class=\"form-control text-center\" autocomplete=\"off\" /></td>";
                //trHtml += "<td><input id=\"Text5\" type=\"text\" class=\"form-control text-center\" dataType=\"null,zzs\" Msg=\"页号格式不正确!\" autocomplete=\"off\" /></td>";
                //trHtml += "<td><input id=\"Text6\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
                //trHtml += "</tr>";
                var tpl = document.getElementById('tpl').innerHTML;
                var trHtml = juicer(tpl, { 'len': 0 });
                $(this).before(trHtml);
                SetIndex();//顺号
                SetMenu2();//绑定右键事件
            }
        }
    };
    var objAddTrBottom = {
        text: "向下插入行",
        func: function () {
            if ($("#tab tbody tr").length > 0) {
                //var trHtml = "<tr>";
                //trHtml += "<td class=\"text-center\">" + 0 + "</td>";
                //// trHtml += "<td><input id=\"Text1\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
                //trHtml += "<td><textarea id=\"Text1\" type=\"text\"  class=\"form-control\"></textarea></td>";
                //trHtml += "<td><input id=\"Text2\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
               
                //trHtml += "<td><textarea  id=\"Text3\" type=\"text\" row=\"3\" class=\"form-control\   onkeyup=\"anxiafangfa(this)\"></textarea></td>";
                //trHtml += "<td><input id=\"Text4\" type=\"text\" class=\"form-control text-center\" autocomplete=\"off\" /></td>";
                //trHtml += "<td><input id=\"Text5\" type=\"text\" class=\"form-control text-center\" dataType=\"null,zzs\" Msg=\"页号格式不正确!\" autocomplete=\"off\" /></td>";
                //trHtml += "<td><input id=\"Text6\" type=\"text\" class=\"form-control\" autocomplete=\"off\" /></td>";
                //trHtml += "</tr>";
                var tpl = document.getElementById('tpl').innerHTML;
                var trHtml = juicer(tpl, { 'len': 0 });
                $(this).after(trHtml);
                SetIndex();//顺号
                SetMenu2();//绑定右键事件
            }
        }
    };
    var sortUpTr = {
        text:"上移",
        func:function () {
            var currentOrderno;//当前排序值
            var tempOrderno;//临时值
            var current = $("#tab tbody tr.selected");//获取当前行
            currentOrderno = current.find('td:eq(2)').text();
            var prev = current.prev();//当前tr的前一个元素
            if (current.index() > 0) {//大于0表示签名还有行，没有到顶
                //下面调换两行的排序值，类似冒泡排序
                tempOrderno = prev.find('td:eq(2)').text();
                prev.find('td:eq(2)').text(currentOrderno);
                current.find('td:eq(2)').text(tempOrderno);
                //把选中行插入到上一行的前面
                current.insertBefore(prev);
            }
        }
    };
    var sortDownTr = {
        text:"下移",
        func: function () {
            var currentOrderno;
            var tempOrderno;
            var current = $("#tab tbody tr.selected");
            currentOrderno = current.find('td:eq(2)').text();
            var next = current.next();//当前tr的下一个元素
            if (next.length > 0) {//大于0表示后面还有行，没有到底
                tempOrderno = next.find('td:eq(2)').text();
                next.find('td:eq(2)').text(currentOrderno);
                current.find('td:eq(2)').text(tempOrderno);
                //把选中行插入到下一行的后面
                current.insertAfter(next);
            }

        }
    };
    var menuTrData = [[objDelTr, objAddTrTop, objAddTrBottom, sortUpTr, sortDownTr]];
    $("#tab tbody tr").smartMenu(menuTrData, {
        name: "menu2",
        beforeShow: function () {

        }
    });
    $("#tab tbody tr").click(function () {
        var ctr = $(this);
        ctr.find("tr.focus").toggleClass("focus");//取消原先选中行
        ctr.toggleClass("focus");//设定当前行为选中行
        //清除以前选择的tr
        $("#tab tbody tr").each(function () {
            var tes = $(this).is(":checked");
            var tes1 = $(this).prop("checked");
            var tes2 = $(this).attr("checked");

            //if ($("#this").is(":checked") == true) { }
            //if ($("#this").prop("checked") == true) { }
            //if ($("#this").attr("checked") == true) { }
        })


        if ($("#tab tbody tr").is(":checked") == true) {
            ctr.prop("checked", false);
        } 
       // ctr.attr("checked", true);
        ctr.select();
    })
}

//生成缺页目录数据
function BuildLackPage() {
    var len = $(".selected a").length;
    if (len > 0) {
        var _FID = $(".selected a").attr("FID");
        var _PageCount = $.trim($("#txtPageCount").val());

        if (_PageCount <= 0) {
            showTip("输入的页数要大于0!", 2);
            return fase;
        }

        _Confirm("确定会删除所有原有目录页及目录缺页记录，并生成新的目录缺页记录，是否继续？", function () {
            $.ajax({
                type: "post",
                url: "ashx/VolumeDir.ashx",
                dataType: "json",
                async: false,
                data: { type: "BuildLackPage", FID: _FID, PageCount: _PageCount },
                success: function (msg) {
                    if (msg.result == 1) {
                        alert("已成功生成" + _PageCount + "条目录缺页记录!");
                    }
                }
            });
        })
    }
}

//顺号
function SetIndex() {
    $("#tab tbody tr").each(function (i) {
        $(this).find("td").eq(0).text(i + 1);

        //$(this).click($(this).select());
    })
}

